package com.chen.mobiletwo.controller;
import com.chen.mobiletwo.entity.ResultEntity;
import com.chen.mobiletwo.service.MessageService;
import com.chen.mobiletwo.service.util.PushMessageService;
import com.chen.mobiletwo.service.util.RedisKeyService;
import com.chen.mobiletwo.tool.JieBaTool;
import com.chen.mobiletwo.tool.KoDoTool;
import com.chen.mobiletwo.tool.OCRTool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/Tool/")
public class ToolController {
    @Resource
    KoDoTool koDoTool;
    @Resource
    PushMessageService pushMessageService;//消息推送服务
    @Resource
    RedisKeyService redisKeyService;

    @Resource
    MessageService messageService;
    @Resource
    OCRTool ocrTool;
    @Resource
    JieBaTool jieBaTool;
    @Value("${project.localfilepath}")
    private String localFilePath;//本地文件存储地址


    private static final Logger logger= LoggerFactory.getLogger(ToolController.class);//日志getLogger需要传入的类名

    @Resource
    private RedisTemplate<String ,String> redisTemplate;//Redis工具类

    @GetMapping("Test")
    private String Test(){
        redisTemplate.opsForValue().set("13800000000","609805");//根据手机号发送验证码
        return ResultEntity.success(redisTemplate.opsForValue().get("13800000000"));
    }

    /**
     * 通用型文件上传七牛云
     * @param file 前端上传的文件
     * @return Json
     */
        @PostMapping("upFile")
    private String upFile(
            @RequestParam("file") MultipartFile file
    ){
        String SuffixName =file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//获取文件后缀名
        String FileNewName= UUID.randomUUID()+SuffixName;//新的文件名
        File dest=new File(localFilePath+FileNewName);//生成新的文件
        if(!dest.getParentFile().exists()){//判定文件夹是否存在
            dest.getParentFile().mkdir();//生成文件夹
        }
        try{
            file.transferTo(dest);//将临时文件转为正式文件
            String koDoString = koDoTool.UploadFile(localFilePath+FileNewName);//上传到七牛云
            return ResultEntity.success(koDoString);//返回七牛云的文件路径字符串
        }catch (Exception e){
            logger.error(e.getMessage());//输出日志
                return ResultEntity.fail(-10001);//返回错误代码
        }
    }

    /**
     * 发送注册邮件
     * @param mailAddress 收件人地址
     * @return String JSON
     */
    @PostMapping("sendMailByRegist")
    private String sendMailByRegist(
            @RequestParam(value = "mailAddress",defaultValue = "") String mailAddress
    ){
        if(mailAddress.equals("")){
            return ResultEntity.fail(-10002);//值为空
        }else {
            if (pushMessageService.pushRegistMail(mailAddress)){
                if (messageService.sendEmail("发送注册邮件。",mailAddress)){
                    return ResultEntity.success(true);
                }else {
                    return ResultEntity.fail(-10005);//数据库插入失败
                }
            }else {
                return ResultEntity.fail(-10003);//邮件发送失败
            }
        }
    }

    /**
     * 判定用户输入的验证码是否正确
     * @param mailAddress 邮箱地址
     * @param code 邮箱验证码
     * @return JSON
     */
    @PostMapping("checkMailCode")
    private String checkMailCode(
            @RequestParam(value = "mailAddress",defaultValue = "") String mailAddress,
            @RequestParam(value = "code",defaultValue = "") String code
    ){
        if(mailAddress.equals("")||code.equals("")){//如果邮箱地址或者邮箱验证是空的
            return ResultEntity.fail(-10002);
        }else {
            return ResultEntity.success(redisKeyService.getValueBykey("mail_Register_RandomCode_"+mailAddress).equals(code));
        }
    }
    @GetMapping("OCR")
    private String OCR(){
        //return ResultEntity.success(ocrTool.OCRBase("C:\\sfz.jpg",2));
        //return ResultEntity.success(ocrTool.OCRBase("C:\\cp.jpeg",3));
        //return ResultEntity.success(ocrTool.OCRBase("C:\\ty.jpg",1));
        return ResultEntity.success(ocrTool.OCRBase("C:\\sfz.jpg",2));

    }

    @GetMapping("Animal")
    private String Animal(){
        //return ResultEntity.success(ocrTool.OCRBase("C:\\sfz.jpg",2));
        //return ResultEntity.success(ocrTool.OCRBase("C:\\cp.jpeg",3));
        //return ResultEntity.success(ocrTool.OCRBase("C:\\ty.jpg",1));
        return ResultEntity.success(ocrTool.Animal("C:\\dw.png"));
    }

    /**
     * 分词
     * @param par 文本内容
     * @return json
     */
    @GetMapping("JieBa")
    private String JieBa(@RequestParam("par")String par){
        return ResultEntity.success(jieBaTool.BaseJieBa(par));
    }
}